﻿using System.Net;
using System.Resources;
using System.Windows.Markup;
using JetBrains.Annotations;

#region Honorbuddy

using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.Helpers;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using CommonBehaviors.Actions;
using Action = Styx.TreeSharp.Action;
#endregion

#region System
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
#endregion

namespace Simcraft
{
    public partial class SimcraftImpl 
    {
        private const string TigereyeBrew = "Tigereye Brew";
        private const string InvokeXuenTheWhiteTiger = "Invoke Xuen, the White Tiger";
        private const string RisingSunKick = "Rising Sun Kick";

        public static int Elusive_Brew_Stack = 128939;

        private static int TIGEREYE_ACTV = 116740;
        private static int TIGEREYE_PASV = 125195;

        #region PvE

        public static Composite MonkCombat()
        {
            return new PrioritySelector(
                new Decorator(ret => SpellManager.HasSpell("Touch of Karma"), Spec_Windwalker()),
                new Decorator(ret => SpellManager.HasSpell("Keg Smash"), Spec_Brewmaster())
                );
        }

        private static int iteCount = 0;
        private static int failCount = 0;

        #region Windwalker
        public static Composite Spec_Windwalker()
        {
            return new PrioritySelector(
                new Decorator(ret => DamageEnabled(), Windwalker_Base()),
                new Decorator(ret => ActiveEnemies >= 3 && AoeEnabled() && DamageEnabled() , Windwalker_AOE()),
                new Decorator(ret => ActiveEnemies < 3 && DamageEnabled(), Windwalker_ST()),
                new Action(delegate(object context) { failCount++; return RunStatus.Success;})
            );
        }

        public static Composite Windwalker_Base()
        {
            return new PrioritySelector(
                //Cast(InvokeXuenTheWhiteTiger, ret => Talent[InvokeXuenTheWhiteTiger].Enabled),
                Cast("Chi Brew", ret => Talent["Chi Brew"].Enabled && Chi.Missing >= 2&&((Spell["Chi Brew"].Charges == 1&&Spell["Chi Brew"].RechargeTime <=10)||Spell["Chi Brew"].Charges == 2)&&Buff[TIGEREYE_PASV].Stack<=16),
                Cast("Tiger Palm", ret => Buff["Tiger Power"].Remains <= 3),
                Cast(TigereyeBrew, ret => Buff[TIGEREYE_ACTV].Down && Buff[TIGEREYE_PASV].Stack == 20),
                Cast(TigereyeBrew, ret => Buff[TIGEREYE_ACTV].Down && Buff[TIGEREYE_PASV].Stack >= 10 && Buff["Serenity"].Up),
                Cast(TigereyeBrew, ret => Buff[TIGEREYE_ACTV].Down && Buff[TIGEREYE_PASV].Stack >= 10 && Cooldown["Fists of Fury"].Up && Chi >= 3 && Debuff[RisingSunKick].Up && Buff["Tiger Power"].Up),
                Cast(TigereyeBrew, ret => Buff[TIGEREYE_ACTV].Down && Buff[TIGEREYE_PASV].Stack >= 10 && Talent["Hurricane Strike"].Enabled && Cooldown["Hurricane Strike"].Up && Chi >= 3 && Debuff[RisingSunKick].Up && Buff["Tiger Power"].Up),
                Cast(TigereyeBrew, ret => Buff[TIGEREYE_ACTV].Down && Chi >= 2 && (Buff[TIGEREYE_PASV].Stack >= 16 || Target.TimeToDie < 40) && Debuff[RisingSunKick].Up && Buff["Tiger Power"].Up),
                Cast(RisingSunKick, ret => Debuff[RisingSunKick].Down || Debuff[RisingSunKick].Remains < 3),
                Cast("Tiger Palm", ret => Buff["Tiger Power"].Down && Debuff[RisingSunKick].Remains > 1 && Energy.TimeToMax > 1),
                Cast("Serenity", ret => Talent["Serenity"].Enabled && Chi >= 2 && Buff["Tiger Power"].Up && Debuff[RisingSunKick].Up),
                new ActionAlwaysFail()
            );
        }

        public static Composite Windwalker_ST()
        {
            return new PrioritySelector(
                Cast("Fists of Fury", ret => Energy.TimeToMax > Spell["Fists of Fury"].ChannelTime && Buff["Tiger Power"].Remains > Spell["Fists of Fury"].ChannelTime && Debuff[RisingSunKick].Remains > Spell["Fists of Fury"].ChannelTime && Buff["Serenity"].Down),
                Cast("Touch of Death", ret => TargetU.HealthPercent < 10),
                Cast("Hurricane Strike", ret => Talent["Hurricane Strike"].Enabled && Energy.TimeToMax > Spell["Hurricane Strike"].ChannelTime && Buff["Tiger Power"].Remains > Spell["Hurricane Strike"].ChannelTime && Debuff[RisingSunKick].Remains > Spell["Hurricane Strike"].ChannelTime && Buff["Energizing Brew"].Down),
                Cast("Energizing Brew", ret => Energy <= 50 && Cooldown["Fists of Fury"].Remains > 6 & (!Talent["Serenity"].Enabled || (!Buff["Serenity"].Down && Cooldown["Serenity"].Remains > 4))),
                Cast(RisingSunKick, ret => !Talent["Chi Explosion"].Enabled),
                Cast("Chi Wave", ret => Talent["Chi Wave"].Enabled && Energy.TimeToMax > 2 && Buff["Serenity"].Down),
                Cast("Chi Burst", ret => Talent["Chi Burst"].Enabled && Energy.TimeToMax > 2 && Buff["Serenity"].Down),
                Cast("Blackout kick", ret => !Talent["Chi Explosion"].Enabled && (Buff["Combo Breaker: Blackout Kick"].Up || Buff["Serenity"].Up)),
                Cast("Chi Explosion", ret => Talent["Chi Explosion"].Enabled && Chi >= 3 && Buff["Combo Breaker: Chi Explosion"].Up),
                Cast("Tiger Palm", ret => Buff["Combo Breaker: Tiger Palm"].Up && Buff["Combo Breaker: Tiger Palm"].Remains <= 2),
                Cast("Blackout Kick", ret => !Talent["Chi Explosion"].Enabled || Chi.Max - Chi < 2),
                Cast("Chi Explosion", ret => Talent["Chi Explosion"].Enabled && Chi >= 3),
                Cast("Expel Harm", ret => Chi.Missing >= 2 && Health.Missing > 8000),
                Cast("Jab", ret => Chi.Missing >= 2),
                new ActionAlwaysFail()
            );
        }

        public static Composite Windwalker_AOE()
        {
            return new PrioritySelector(
                Cast("Chi Explosion", ret => Chi >= 4),
                Cast("Rushing Jade Wind", ret => Talent["Rushing Jade Wind"].Enabled),
                Cast("Rising Sun kick", ret => !Talent["Rushing Jade Wind"].Enabled && (Chi == Chi.Max)),
                Cast("Fists of Fury", ret => Energy.TimeToMax > Spell["Fists of Fury"].ChannelTime && Buff["Tiger Power"].Remains > Spell["Fists of Fury"].ChannelTime && Debuff[RisingSunKick].Remains > Spell["Fists of Fury"].ChannelTime && Buff["Serenity"].Down),
                Cast("Touch of Death", ret => Target.Health.Percent < 10),
                Cast("Hurricane Strike", ret => Talent["Rushing Jade Wind"].Enabled && Talent["Hurricane Strike"].Enabled && Energy.TimeToMax > Spell["Hurricane Strike"].ChannelTime && Buff["Tiger Power"].Remains > Spell["Hurricane Strike"].ChannelTime && Debuff[RisingSunKick].Remains > Spell["Hurricane Strike"].ChannelTime && Buff["Energizing Brew"].Down),
                Cast("Chi Wave", ret => Talent["Chi Wave"].Enabled && Energy.TimeToMax > 2 && Buff["Serenity"].Down),
                Cast("Chi Burst", ret => Talent["Chi Burst"].Enabled && Energy.TimeToMax > 2 && Buff["Serenity"].Down),
                Cast("Blackout kick", ret => Talent["Rushing Jade Wind"].Enabled && !Talent["Chi Explosion"].Enabled && (Buff["Combo Breaker: Blackout Kick"].Up || Buff["Serenity"].Up)),
                Cast("Chi Explosion", ret => Talent["Rushing Jade Wind"].Enabled && Talent["Chi Explosion"].Enabled && Chi >= 3 && Buff["Combo Breaker: Chi Explosion"].Up),
                Cast("Tiger Palm", ret => Talent["Rushing Jade Wind"].Enabled && Buff["Combo Breaker: Tiger Palm"].Up && Buff["Combo Breaker: Tiger Palm"].Remains <= 2),
                Cast("Blackout Kick", ret => Talent["Rushing Jade Wind"].Enabled && !Talent["Chi Explosion"].Enabled || Chi.Max - Chi < 2),
                Cast("Chi Explosion", ret => Talent["Rushing Jade Wind"].Enabled && Talent["Chi Explosion"].Enabled || Chi >= 3),
                Cast("Spinning Crane Kick", ret => !Talent["Rushing Jade Wind"].Enabled),
                Cast("Jab", ret => Talent["Rushing Jade Wind"].Enabled && Chi.Max - Chi >= 2),
                new ActionAlwaysFail()
            );
        }

        #endregion
        #region Brewmaster
        public static Composite Spec_Brewmaster()
        {
            return new PrioritySelector(

                new Decorator(ret => DamageEnabled(true), Brewmaster_Base()),
                new Decorator(ret => DamageEnabled() && ActiveEnemies >= 3 && AoeEnabled(true), Brewmaster_AOE()),
                new Decorator(ret => DamageEnabled(), Brewmaster_ST()),
                new ActionAlwaysSucceed()
            );
        }

        public static Composite Brewmaster_Base()
        {

            return new PrioritySelector(
                Cast("Chi Brew", ret => Talent["Chi Brew"].Enabled && (Chi.Max - Chi) >= 2 && Buff[Elusive_Brew_Stack].Stack <= 10),
                Cast("Serenity", ret => Talent["Serenity"].Enabled && Energy <= 40),
                Cast("Expel Harm", ret => Health.Percent < 35),
                new ActionAlwaysFail()
            );
        }

        public static Composite Brewmaster_ST()
        {
            return new PrioritySelector(
                Cast("Blackout Kick", ret => Buff["Shuffle"].Down && Chi >= 2),
                Cast("Purifying Brew", ret => Buff["Serenity"].Up),
                Cast("Keg Smash", ret => Chi.Missing >= 2 && Buff["Serenity"].Down && AoeEnabled()),
                Cast("Chi Burst", ret => Talent["Chi Burst"].Enabled && Energy.TimeToMax > 3),
                Cast("Chi Wave", ret => Talent["Chi Wave"].Enabled && Energy.TimeToMax > 3),
                Cast("Chi Explosion", ret => Talent["Chi Explosion"].Enabled && Chi >= 3),
                Cast("Blackout Kick", ret => !Talent["Chi Explosion"].Enabled && Buff["Shuffle"].Remains <= 3 && Cooldown["Keg Smash"].Remains >= 1.5),
                Cast("Blackout Kick", ret => !Talent["Chi Explosion"].Enabled && Buff["Serenity"].Up),
                Cast("Blackout Kick", ret => !Talent["Chi Explosion"].Enabled && Chi >= 4),
                Cast("Expel Harm", ret => Chi.Missing >= 1 && Cooldown["Keg Smash"].Remains >= 1.5),
                Cast("Jab", ret => Chi.Missing >= 1 && Cooldown["Keg Smash"].Remains >= 1.5 && Cooldown["Expel Harm"].Remains >= 1.5),
                Cast("Tiger Palm", ret => (Energy.Current + (Energy.Regen * (Cooldown["Keg Smash"].Remains))) >= 40),
                Cast("Tiger Palm", ret => Cooldown["Keg Smash"].Remains >= 1.5),
                new ActionAlwaysFail()
            );
        }

        public static Composite Brewmaster_AOE()
        {
            return new PrioritySelector(
                Cast("Breath of Fire", ret => Chi >= 3 && Buff["Shuffle"].Remains >= 6 && UnfriendlysMissingDebuff("Breath of Fire", 12) > 4),
                Cast("Chi Explosion", ret => Talent["Chi Explosion"].Enabled && Chi >= 4),
                Cast("Rushing Jade Wind", ret => Talent["Rushing Jade Wind"].Enabled && Chi.Max - Chi >= 1),
                Cast("Keg Smash", ret => Chi.Missing >= 2 && Buff["Serenity"].Down),
                Cast("Chi Burst", ret => Talent["Chi Burst"].Enabled && Energy.TimeToMax > 3),
                Cast("Chi Wave", ret => Talent["Chi Wave"].Enabled && Energy.TimeToMax > 3),
                Cast("Chi Explosion", ret => Talent["Chi Explosion"].Enabled && Chi >= 3),
                Cast("Blackout Kick", ret => !Talent["Chi Explosion"].Enabled && Buff["Shuffle"].Remains <= 3 && Cooldown["Keg Smash"].Remains >= 1.5),
                Cast("Blackout Kick", ret => !Talent["Chi Explosion"].Enabled && Buff["Serenity"].Up),
                Cast("Blackout Kick", ret => !Talent["Chi Explosion"].Enabled && Chi >= 4),
                SelfCast("Expel Harm", ret => Chi.Missing >= 1 && Cooldown["Keg Smash"].Remains >= 1.5 && (Energy.Current + (Energy.Regen * (Cooldown["Keg Smash"].Remains))) >= 40),
                Cast("Spinning Crane Kick", ret => !Talent["Rushing Jade Wind"].Enabled && Chi.Missing >= 1),
                Cast("Jab", ret => Chi.Missing >= 1 && Cooldown["Keg Smash"].Remains >= 1.5 && Cooldown["Expel Harm"].Remains >= 1.5),
                Cast("Tiger Palm", ret => Talent["Rushing Jade Wind"].Enabled && (Energy.Current + (Energy.Regen * (Cooldown["Keg Smash"].Remains))) >= 40),
                Cast("Tiger Palm", ret => Talent["Rushing Jade Wind"].Enabled && Cooldown["Keg Smash"].Remains >= 1.5),
                new ActionAlwaysFail()
            );
        }
        #endregion
        #endregion 

        #region PvP
        public static Composite MonkPvPCombat()
        {
            return new PrioritySelector(
                new Decorator(ret => SpellManager.HasSpell("Touch of Karma"), Spec_Windwalker_PvP()),
                new ActionAlwaysSucceed(),
                new Decorator(ret => SpellManager.HasSpell("Keg Smash"), Spec_Brewmaster())
                );
        }

        public static Composite Spec_Windwalker_PvP()
        {
            return new PrioritySelector(
                new Action(delegate(object context)
            {
                ite++;
                Logging.Write("ite" + ite);
                return RunStatus.Failure;
            }),
                Cast("Touch of Death", ret => TargetU.HealthPercent < 10),
                Cast("Rising Sun Kick", ret => Chi >= 2),
                Cast("Blackout Kick", ret => Buff["Combo Breaker: Blackout Kick"].Up && Buff["Combo Breaker: Blackout Kick"].Remains <= 2),    
                Cast("Tiger Palm", ret => Buff["Tiger Power"].Down),             
                Cast("Tiger Palm", ret => Buff["Combo Breaker: Tiger Palm"].Up && Buff["Combo Breaker: Tiger Palm"].Remains <= 2),       
                Cast("Blackout Kick", ret => Chi >= 2),
                Cast("Expel Harm", ret => Chi.Missing >= 2 && Health.Missing > 8000),
                Cast("Jab", ret => Energy >= 50 && Chi.Missing >= 2),
                Cast("Chi Explosion", ret => Chi >= 2),
                new ActionAlwaysSucceed()
            );
        }
        #endregion


    }
}
